package io.renren.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.bean.ExercisesQuestionInfo;
import io.renren.dao.QuestionErrorDao;
import io.renren.entity.QuestionErrorEntity;
import io.renren.util.ExercisesUtil;
import io.renren.service.QuestionErrorService;
import io.renren.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/***
 * 错题
 * @author 霍礼康
 */
@Service("questionErrorService")
public class QuestionErrorServiceImpl extends ServiceImpl<QuestionErrorDao, QuestionErrorEntity> implements QuestionErrorService {

    @Autowired
    private QuestionService questionService;

    /***
     * 获取错题题目/收藏题目
     * @param studentId 学员id
     * @param subjectId 科目id
     * @param errorQuestionNumber 错题总数
     * @param sort 序号
     * @param type 针对错题练习 1为错题题目练习 2为收藏题目练习(错题练习一定要传type)
     * @return
     */
    @Override
    public ExercisesQuestionInfo getErrorQuestion(Long studentId, Integer subjectId, Integer errorQuestionNumber, Integer sort, Integer type) {
        List<Map<String, Object>> errorQuestion = baseMapper.getErrorQuestion(studentId, subjectId, errorQuestionNumber, sort, type);
        ExercisesQuestionInfo bean = new ExercisesQuestionInfo();
        bean.setSumQuestionNumber(errorQuestionNumber);
        bean.setExercisesType(3);
        bean.setAnswerNumber(sort - 1);
        return ExercisesUtil.checkQuestion(3, errorQuestion, bean);
    }

    /***
     * 获取错题/收藏题目的数量
     * @param subjectId 科目id
     * @param studentId 学员id
     * @param type  针对错题练习 1为错题题目练习 2为收藏题目练习(错题练习一定要传type)
     * @return
     */
    @Override
    public Integer getQuetionNumber(Integer subjectId, Long studentId, Integer type) {
        List<Map<String, Object>> list = baseMapper.getQuestionNumber(subjectId, studentId, type);
        return list == null ? 0 : list.size();
    }

    /***
     * 查询出错题实体类
     * @param studentId 学员id
     * @param questionId 问题id
     * @return
     */
    @Override
    public QuestionErrorEntity selectQuestionError(Long studentId, Integer questionId) {
        EntityWrapper<QuestionErrorEntity> entityEntityWrapper = new EntityWrapper<>();
        entityEntityWrapper.eq("student_id", studentId);
        entityEntityWrapper.eq("question_id", questionId);
        QuestionErrorEntity questionErrorEntity = this.selectOne(entityEntityWrapper);
        return questionErrorEntity;
    }



    /***
     * 题入库
     * @param studentId 学员id
     * @param questionId 题目id
     */
    @Override
    public void errorQuestionStorage(Long studentId, Integer questionId) {
        QuestionErrorEntity questionErrorEntity = null;
        EntityWrapper<QuestionErrorEntity> entityEntityWrapper = new EntityWrapper<>();
        entityEntityWrapper.eq("student_id", studentId);
        entityEntityWrapper.eq("question_id", questionId);
        questionErrorEntity = this.selectOne(entityEntityWrapper);
        if (questionErrorEntity == null) {
            questionErrorEntity = new QuestionErrorEntity();
            questionErrorEntity.setCreateBy(studentId);
            questionErrorEntity.setCreateDate(new Date());
            questionErrorEntity.setType(1);
            questionErrorEntity.setStudentId(studentId);
            questionErrorEntity.setLastModifyDate(new Date());
            questionErrorEntity.setQuestionId(questionId);
            this.insert(questionErrorEntity);
        }
    }

    /***
     * 查询出是否添加成功
     * @param studentId 学生id
     * @param questionId 问题id
     * @return
     */
    @Override
    public Integer insertErrorQuestion(Long studentId, Integer questionId) {

        EntityWrapper<QuestionErrorEntity> entityEntityWrapper = new EntityWrapper<>();
        entityEntityWrapper.eq("student_id", studentId);
        entityEntityWrapper.eq("question_id", questionId);
        entityEntityWrapper.eq("type", 2);
        QuestionErrorEntity questionError = this.selectOne(entityEntityWrapper);
        if (questionError == null) {
            questionError = new QuestionErrorEntity();
            questionError.setQuestionId(questionId);
            questionError.setLastModifyDate(new Date());
            questionError.setStudentId(studentId);
            questionError.setType(2);
            questionError.setCreateDate(new Date());
            questionError.setCreateBy(studentId);
            insert(questionError);
            return 1;
        } else {
            return 2;
        }


    }

    /***
     * 清空收藏题目
     * @param studentId 学员id
     * @param subjectId 科目id
     * @return R
     */
    @Override
    public void resetCollectionQuestion(Long studentId, Integer subjectId) {
          baseMapper.resetCollectionQuestion(studentId,subjectId);
    }
}
